package launcher

import (
	"errors"
	"flag"
	"fmt"
	"ylm.minidb/backend/utils"
)

const (
	_NET         = "tcp"
	_ADDRESS     = ":8080"
	_DEFAULT_MEM = (1 << 20) * 64 // 64 MB
)

const (
	_KB = 1 << 10
	_MB = 1 << 20
	_GB = 1 << 30
)

var (
	ErrInvalidMem = errors.New("Invalid Memory Size.")
)

func openDB(path string, mem int64) {
	// TODO 具体实现
}

func createDB(path string) {
	// TODO 具体实现
}

func main() {
	open := flag.String("open", "", "-open DBPath")
	create := flag.String("create", "", "-create DBPath")
	memStr := flag.String("mem", "", "-mem 64MB")
	flag.Parse()

	if *open != "" {
		openDB(*open, parseMem(*memStr))
		return
	}
	if *create != "" {
		createDB(*create)
		return
	}
	fmt.Println("Usage: launcher (open|create) DBPath")
}

func parseMem(memStr string) int64 {
	if memStr == "" {
		return _DEFAULT_MEM
	}
	length := len(memStr)

	memeUint := memStr[length-2:]
	memeNum, err := utils.StrToUint64(memStr[length-2:])
	if err != nil {
		panic(err)
	}
	switch memeUint {
	case "KB":
		return int64(memeNum) * _KB
	case "MB":
		return int64(memeNum) * _MB
	case "GB":
		return int64(memeNum) * _GB
	default:
		panic(ErrInvalidMem)
	}
}
